import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:state_manage/counter/counter_bloc.dart';
import 'package:state_manage/counter/event.dart';

/// 测试页面
class CounterTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Counter',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: BlocProvider(
          // 使用 BlocProvider
          create: (context) => CounterBloc(),
          child: MyHomePage(title: 'Counter'),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    // 获取Bloc
    final CounterBloc bloc = BlocProvider.of<CounterBloc>(context);

    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: BlocBuilder<CounterBloc, int>(
          builder: (context, count) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'You have pushed the button this many times:',
                  ),
                  Text(
                    '$count',
                    style: Theme.of(context).textTheme.display1,
                  ),
                ],
              ),
            );
          },
        ),
        floatingActionButton: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            // 加
            Padding(
              padding: EdgeInsets.symmetric(vertical: 5.0),
              child: FloatingActionButton(
                onPressed: () {
                  bloc.add(CounterEvent.increment);
                },
                tooltip: 'Increment',
                child: Icon(Icons.add),
              ),
            ),
            // 减
            Padding(padding: EdgeInsets.symmetric(vertical: 5.0),
              child: FloatingActionButton(
                onPressed: () {
                  bloc.add(CounterEvent.decrement);
                },
                tooltip: 'Decrement',
                child: Icon(Icons.remove),
              ),)
          ],
        ));
  }
}
